package system;

import java.util.HashMap;
import java.util.Map;

/**
 * Class Html
 * 
 * @author Rezan Achmad
 */
public class Html {

    public static String dropDownList(String name, String selected, HashMap<String, String> listData) {
        return dropDownList(name, selected, listData, null);
    }

    public static String dropDownList(String name, String selected, HashMap<String, String> listData, HashMap<String, String> htmlOptions) {
        StringBuilder sb = new StringBuilder();

        // Open select tag
        sb.append("<select name=\"");

        // Write name
        sb.append(name).append("\" ");

        // Write htmlOptions
        if (htmlOptions != null && htmlOptions.size() > 0) {
            for (Map.Entry<String, String> entry : htmlOptions.entrySet()) {
                sb.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\" ");
            }
        }

        sb.append(">");

        // Write option collection
        for (Map.Entry<String, String> entry : listData.entrySet()) {
            // Open option tag
            sb.append("<option value=\"");

            // Write key
            sb.append(entry.getKey()).append("\"");

            // Set selected
            if (entry.getKey().equals(selected)) {
                sb.append(" selected=\"selected\"");
            }

            sb.append(">");

            // Write value
            sb.append(entry.getValue());

            // Close option tag
            sb.append("</option>");
        }

        // Close select tag
        sb.append("</select>");

        return sb.toString();
    }
}
